package com.cn.ccp.action.course;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseIssueBean;
import com.cn.ccp.util.CCPContants;

/**
 * 学生课程留言action
 */
public class StduentCourseIssueAction extends BaseAction {
	private Logger log = Logger.getLogger(StduentCourseIssueAction.class);
	private String content ;
	
	private String showListInfo = "";
	
	private String courseName ;
	private String courseID ;
	
	public StduentCourseIssueAction(){
		setPageSize(5);
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String cID = varMap.get("id");
		if(cID != null  && !cID.equals("")){
			courseID = cID ;
//			CCPContants.setValue("IssuecourseId", courseID) ;
			showListInfo = "";
			courseName = getCourseService().getBeanById(courseID).getCourse_name();
			getpagedetail();
		}
		log.info("----------courseID = "+courseID);
	}
	
	public void save(){
		content = content.trim();
//		if(courseID == null || courseID.equals("") ){
//			courseID = CCPContants.getValue("IssuecourseId");
//		}
		if(content == null  || content.equals("")){
			showMsg("contentid", "online_answer_page_submit_content_null");
		}else{
			boolean flag = getCourseIssueService().saveStudent(courseID, content);
			if(flag){
				showMsg("contentid", "online_answer_page_submit_student_success");
				content = "";
				setCurrentPage(1);
			}else{
				showMsg("contentid", "online_answer_page_submit_student_fauire");
			}
		}
		getpagedetail();
	}
	
	
	@Override
	public void getpagedetail() {
//		if(courseID == null || courseID.equals("") ){
//			courseID = CCPContants.getValue("IssuecourseId");
//		}
		setItemCount(getCourseIssueService().getStudentCount(courseID));
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		if(start < 0)
			start = 0 ;
		
//		log.info("currentpage = "+currentpage);
//		log.info("pagesize = "+pagesize);
//		log.info("start = "+start);
		List<CourseIssueBean> questionList = getCourseIssueService().getStudentCourseIssueList(courseID, "1" , start, pagesize);
		List<CourseIssueBean> answerList = getCourseIssueService().getStudentCourseIssueList(courseID, "2" , start, pagesize);
		if(questionList.size() > 0){
			LinkedHashMap<Integer, CourseIssueBean> map = new LinkedHashMap<Integer, CourseIssueBean>();
			for (int i = 0; i < questionList.size(); i++) {
				CourseIssueBean bean = questionList.get(i);
				map.put(bean.getId(), bean);
			}
			for(CourseIssueBean bean : answerList){
				CourseIssueBean qbean = map.get(Integer.parseInt(bean.getIssue_id()));
				if(qbean != null ){
					qbean.getCcList().add(bean);
					map.put(Integer.parseInt(bean.getIssue_id()), qbean);
				}
			}
			Iterator<Integer> iters = map.keySet().iterator();
			StringBuffer sb = new StringBuffer();
			while(iters.hasNext()){
				int id = iters.next();
				CourseIssueBean bean = map.get(id);
				try {
					boolean isToday = CCPContants.isToday(CCPContants.sdf2.parse(bean.getWrite_time()));
					String msg = bean.getWrite_time();
					if(isToday){
						msg = getMessages("label_today")+" <small>"+bean.getWrite_time().split(" ")[1]+"</small>";
					}
					sb.append("<div class=\"answer\" >"+
							"<h5>"+
							bean.getStudentName()+"："+msg+
							"</h5>"+
							"<p>"+bean.getIssue()+"</p>"+
							"<hr />"+
					"</div>");
				} catch (Exception e) {
					// TODO: handle exception
				}
				for (int i = 0; i <bean.getCcList().size(); i++) {
					try {
						CourseIssueBean taBean = bean.getCcList().get(i);
						sb.append("<div class=\"issue\" style='margin-left: 40px;'>"+
									"<h5>"+
										taBean.getMsg()+
									"</h5>"+
									"<p>"+taBean.getIssue()+"</p>"+
									"<hr />"+
								"</div>");
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
			showListInfo = sb.toString();
		}
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getShowListInfo() {
		return showListInfo;
	}

	public void setShowListInfo(String showListInfo) {
		this.showListInfo = showListInfo;
	}

	public String getCourseID() {
		return courseID;
	}

	public void setCourseID(String courseID) {
		this.courseID = courseID;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	
}
